/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmManagerEmployee.java
 *
 * Created on Dec 16, 2011, 2:38:04 PM
 */
package vn.edu.nuce.lms.ui;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import vn.edu.nuce.lms.dao.BaseDao;
import vn.edu.nuce.lms.dao.EmployeeDao;

/**
 *
 * @author Dell
 */
public class FrmManagerEmployee extends javax.swing.JFrame {
   DefaultTableModel model;
    Connection conn;
    Statement st;
    PreparedStatement pstmt;
    ResultSet rs;
    BaseDao connect = new BaseDao();
    EmployeeDao nhap=new EmployeeDao();
    /** Creates new form FrmManagerEmployee */
    public FrmManagerEmployee() {
        initComponents();
        model = (DefaultTableModel)tblManagerEmployee.getModel();
        nhap.Load(tblManagerEmployee);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblManagerEmployee = new javax.swing.JTable();
        lblQuanly = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        cboSearch = new javax.swing.JComboBox();
        txtSearch = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblManagerEmployee.setForeground(new java.awt.Color(0, 0, 255));
        tblManagerEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Name", "Dayofbirth", "Level", "Email", "Phone", "Join Day", "Day of leave"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblManagerEmployee.setName("tblManagerEmployee"); // NOI18N
        jScrollPane1.setViewportView(tblManagerEmployee);

        lblQuanly.setFont(new java.awt.Font("Tahoma", 0, 36));
        lblQuanly.setForeground(new java.awt.Color(255, 0, 0));
        lblQuanly.setText("Quản Lý Nhân Viên");
        lblQuanly.setName("lblQuanly"); // NOI18N

        btnAdd.setForeground(new java.awt.Color(0, 51, 204));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/nuce/lms/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setForeground(new java.awt.Color(0, 51, 204));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/nuce/lms/image/index.jpg"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.setName("btnEdit"); // NOI18N

        btnDelete.setForeground(new java.awt.Color(0, 51, 204));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/nuce/lms/image/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.setName("btnDelete"); // NOI18N

        btnSearch.setForeground(new java.awt.Color(0, 51, 204));
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vn/edu/nuce/lms/image/search.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cboSearch.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cboSearch.setForeground(new java.awt.Color(0, 0, 204));
        cboSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Name" }));
        cboSearch.setName("cboSearch"); // NOI18N

        txtSearch.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtSearch.setForeground(new java.awt.Color(0, 0, 153));
        txtSearch.setName("txtSearch"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 656, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(47, 47, 47)
                            .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(35, 35, 35)
                            .addComponent(btnSearch))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(190, 190, 190)
                            .addComponent(lblQuanly, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(173, 173, 173)
                        .addComponent(btnAdd)
                        .addGap(30, 30, 30)
                        .addComponent(btnEdit)
                        .addGap(28, 28, 28)
                        .addComponent(btnDelete)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(lblQuanly, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(53, 53, 53))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-706)/2, (screenSize.height-470)/2, 706, 470);
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
    FrmAddEmployee frmadd=new FrmAddEmployee();
    frmadd.setVisible(true);
}//GEN-LAST:event_btnAddActionPerformed

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
// TODO add your handling code here:
         //Xóa dữ liệu trong data Vector object
    model.getDataVector().removeAllElements();
    //Cập nhật lại jTable để hiển thị trên màn hình
   tblManagerEmployee.repaint();
    if (txtSearch.getText().length() == 0) {
        JOptionPane.showMessageDialog(null, "Dien vao tu khoa can tim!");
        return;
    }
    if (txtSearch.getText().equals("all")) {
        nhap.Load(tblManagerEmployee);
    }
    if (cboSearch.getSelectedItem().toString().equals("ID")) {

        try {
            conn = connect.getConnection();
            String strSQL ="SELECT EmId,EmName,EmDayOfBirth,EmLevel,EmEmail,EmPhone,EmJoinDay,EmDayOfLeave from Employee where EmId=?";
            Vector v = new Vector();


            pstmt = conn.prepareStatement(strSQL);
            pstmt.setString(1, txtSearch.getText());
            rs = pstmt.executeQuery();
            try {

                while (rs.next()) {
                    v = new Vector();
                    v.addElement(rs.getInt(1));
                    v.addElement(rs.getString(2));
                    v.addElement(rs.getDate(3));
                    v.addElement(rs.getInt(4));
                    v.addElement(rs.getString(5));
                    v.addElement(rs.getString(6));
                    v.addElement(rs.getDate(7));
                    v.addElement(rs.getInt(8));
                    model.addRow(v);
                }
                //set lai model cho jtable
                tblManagerEmployee.setModel(model);
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error:" + ex.getMessage());
            return;
        }
    }
    if (cboSearch.getSelectedItem().toString().equals("Name")) {
        try {
            conn = connect.getConnection();
            String strSQL = "select * from Employee where EmName like ?";
            Vector v = new Vector();


            pstmt = conn.prepareStatement(strSQL);
            pstmt.setString(1, "%" + txtSearch.getText() + "%");
            rs = pstmt.executeQuery();
            try {

                while (rs.next()) {
                    v = new Vector();
                    v.addElement(rs.getInt(1));
                    v.addElement(rs.getString(2));
                    v.addElement(rs.getDate(3));
                    v.addElement(rs.getInt(4));
                    v.addElement(rs.getString(5));
                    v.addElement(rs.getString(6));
                    v.addElement(rs.getDate(7));
                    v.addElement(rs.getInt(8));
                    model.addRow(v);
                }
                //set lai model cho jtable
                tblManagerEmployee.setModel(model);
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error:" + ex.getMessage());
            return;
        }
    }
}//GEN-LAST:event_btnSearchActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmManagerEmployee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmManagerEmployee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmManagerEmployee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmManagerEmployee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmManagerEmployee().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblQuanly;
    private javax.swing.JTable tblManagerEmployee;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
